package com.governtalk.backend.repository;

import com.governtalk.backend.entity.WrongQuestion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface WrongQuestionRepository extends JpaRepository<WrongQuestion, Long> {
    
    /**
     * 根据用户ID分页查询错题
     */
    Page<WrongQuestion> findByUserIdOrderByErrorTimeDesc(Long userId, Pageable pageable);
    
    /**
     * 根据用户ID查询所有错题
     */
    List<WrongQuestion> findByUserIdOrderByErrorTimeDesc(Long userId);
    
    /**
     * 根据用户ID和题目ID查询错题
     */
    Optional<WrongQuestion> findByUserIdAndQuestionId(Long userId, Long questionId);
    
    /**
     * 根据用户ID和复习状态查询错题
     */
    Page<WrongQuestion> findByUserIdAndIsReviewedOrderByErrorTimeDesc(Long userId, Boolean isReviewed, Pageable pageable);
    
    /**
     * 根据用户ID统计错题数量
     */
    long countByUserId(Long userId);
    
    /**
     * 根据用户ID统计已复习错题数量
     */
    long countByUserIdAndIsReviewed(Long userId, Boolean isReviewed);
    
    /**
     * 根据用户ID和关键词搜索错题
     */
    @Query("SELECT wq FROM WrongQuestion wq " +
           "JOIN Question q ON q.id = wq.questionId " +
           "WHERE wq.userId = :userId " +
           "AND (q.content LIKE %:keyword% OR wq.errorReason LIKE %:keyword% OR wq.reviewNotes LIKE %:keyword%) " +
           "ORDER BY wq.errorTime DESC")
    Page<WrongQuestion> searchByUserIdAndKeyword(@Param("userId") Long userId, 
                                                 @Param("keyword") String keyword, 
                                                 Pageable pageable);
    
    /**
     * 根据用户ID和题型查询错题
     */
    @Query("SELECT wq FROM WrongQuestion wq " +
           "JOIN Question q ON q.id = wq.questionId " +
           "WHERE wq.userId = :userId " +
           "AND q.type = :type " +
           "ORDER BY wq.errorTime DESC")
    Page<WrongQuestion> findByUserIdAndQuestionType(@Param("userId") Long userId, 
                                                    @Param("type") String type, 
                                                    Pageable pageable);
    
    /**
     * 根据用户ID和题目分类查询错题
     */
    @Query("SELECT wq FROM WrongQuestion wq " +
           "JOIN Question q ON q.id = wq.questionId " +
           "WHERE wq.userId = :userId " +
           "AND q.categoryId = :categoryId " +
           "ORDER BY wq.errorTime DESC")
    Page<WrongQuestion> findByUserIdAndQuestionCategory(@Param("userId") Long userId, 
                                                        @Param("categoryId") Long categoryId, 
                                                        Pageable pageable);
    
    /**
     * 根据用户ID删除错题
     */
    void deleteByUserIdAndQuestionId(Long userId, Long questionId);
    
    /**
     * 根据用户ID清空所有错题
     */
    void deleteByUserId(Long userId);
}
